﻿using Apps.Common;
using Apps.Models;
using System.Linq;
using System.Collections.Generic;
using System;
using Apps.Models.PL;

namespace Apps.BLL.PL
{
    public partial class PL_RC_TRANSPORTBLL
    {

        public override List<PL_RC_TRANSPORTModel> CreateModelList(ref IQueryable<PL_RC_TRANSPORT> queryData)
        {

            List<PL_RC_TRANSPORTModel> modelList = (from r in queryData
                                                    select new PL_RC_TRANSPORTModel
                                                    {
                                                        STATUS = r.STATUS,
                                                        CREATE_TIME = r.CREATE_TIME,
                                                        CREATE_USER_ID = r.CREATE_USER_ID,
                                                        CREATE_TYPE = r.CREATE_TYPE,
                                                        WEIGHT = r.WEIGHT,
                                                        TRANSPORT_ID = r.TRANSPORT_ID,
                                                        SUPPLIER_ID = r.SUPPLIER_ID,
                                                        MATERIEL_ID = r.MATERIEL_ID,
                                                        MINE_ID = r.MINE_ID,
                                                        CAR_ID = r.CAR_ID,
                                                        CarName = r.PUB_CAR.LPR,
                                                        RC_TRANSPORT_ID = r.RC_TRANSPORT_ID,
                                                        MATERIELName = r.PUB_MATERIEL.MATERIEL_NAME,
                                                        MINEName =  string.IsNullOrEmpty(r.MINE_ID) ? "煤矿" : r.PUB_MINE.MINE_NAME,
                                                        SUPPLIERName = r.PUB_SUPPLIER.SUPPLIER_NAME,
                                                        TRANSPORTName = r.PUB_TRANSPORT.TRANSPORT_NAME
                                                    }).ToList();
            List<PL_RC_TRANSPORTModel> tmpModelList;
            using (DBContainer db = new DBContainer())
            {
                tmpModelList = (from r in modelList
                                join u in db.SysUser on r.CREATE_USER_ID equals u.Id into temp
                                from tt in temp.DefaultIfEmpty()
                                select new PL_RC_TRANSPORTModel
                                {
                                    STATUS = r.STATUS,
                                    CREATE_TIME = r.CREATE_TIME,
                                    CREATE_USER_ID = tt == null ? "" : tt.TrueName,
                                    CREATE_TYPE = r.CREATE_TYPE,
                                    WEIGHT = r.WEIGHT,
                                    TRANSPORT_ID = r.TRANSPORT_ID,
                                    SUPPLIER_ID = r.SUPPLIER_ID,
                                    MATERIEL_ID = r.MATERIEL_ID,
                                    MINE_ID = r.MINE_ID,
                                    CAR_ID = r.CAR_ID,
                                    CarName = r.CarName,
                                    RC_TRANSPORT_ID = r.RC_TRANSPORT_ID,
                                    MATERIELName = r.MATERIELName,
                                    MINEName = r.MINEName,
                                    SUPPLIERName = r.SUPPLIERName,
                                    TRANSPORTName = r.TRANSPORTName
                                }).ToList();

            }

            return tmpModelList;
        }

        public override PL_RC_TRANSPORTModel GetById(object id)
        {
            if (IsExists(id))
            {

                PL_RC_TRANSPORT entity = m_Rep.GetById(id);
                PL_RC_TRANSPORTModel model = new PL_RC_TRANSPORTModel();
                model.STATUS = entity.STATUS;
                model.CREATE_TIME = entity.CREATE_TIME;
                model.CREATE_USER_ID = entity.CREATE_USER_ID;
                model.CREATE_TYPE = entity.CREATE_TYPE;
                model.WEIGHT = entity.WEIGHT;
                model.TRANSPORT_ID = entity.TRANSPORT_ID;
                model.SUPPLIER_ID = entity.SUPPLIER_ID;
                model.MATERIEL_ID = entity.MATERIEL_ID;
                model.MINE_ID = entity.MINE_ID;
                model.CAR_ID = entity.CAR_ID;
                model.RC_TRANSPORT_ID = entity.RC_TRANSPORT_ID;
                model.MATERIELName = entity.PUB_MATERIEL.MATERIEL_NAME;
                model.MINEName = entity.PUB_MINE.MINE_NAME;
                model.SUPPLIERName = entity.PUB_SUPPLIER.SUPPLIER_NAME;
                model.TRANSPORTName = entity.PUB_TRANSPORT.TRANSPORT_NAME;
                return model;
            }

            else
            {
                return null;
            }
        }
    }
}
